/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: ConfirmSend
 * Author:   cakin
 * Date:     2020/4/25
 * Description: 生产者：普通confirm模式
 */
package com.rabbitmq.productconfirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.util.ConnectionUtils;
import lombok.extern.slf4j.Slf4j;


import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName: ConfirmSend
 * @Description: 生产者：普通confirm模式
 * @Date: 2020/4/25
 * @Author: cakin
 */
@Slf4j
public class ConfirmSend {
    /**
     * 队列名称
     */
    private static final String QUEUE_NAME = "test_queue_confirm1";

    public static void main( String[] args ) throws IOException, TimeoutException, InterruptedException {
        // 获取连接
        Connection connection = ConnectionUtils.getConnection();
        // 从连接开一个通道
        Channel channel = connection.createChannel();
        // 声明一个队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        channel.confirmSelect(); // 开始confirm模式

        // 发送消息
        String message = "hello, confirm message";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

        if (channel.waitForConfirms()) {
            log.info(" [x] Sent message : '" + message + "' ok ");
        } else {
            log.info(" [x] Sent message : '" + message + "' fail ");
        }

        channel.close();
        connection.close();
    }
}

